home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0079_Virtual Coords.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  2KB  |  59 lines

  1. {
  2. You can do a basic horizontal starfeild where all you need is an array of
  3. x,y locations, a routine to draw the stars in the next position, a routine
  4. to remove the old stars, and a routine to update the position array.  Or one
  5. routine to do all that. That gets boring once you write one.  So you want one
  6. you can fly into. Now you need x,y and a z coord.  To get the virtual x,y
  7. screen coords for each point, take their 3d-x coord and divide by the 3d-z
  8. coord, and do the same for the y.  This will give you a real number, and
  9. reals are slow so here's an example of just that math using only integers.
  10. }
  11.   X  : Integer; {3d x coord  -maxint to maxint, left to right}
  12.   Y  : Integer;    {y            "         "    top to bottom}
  13.   Z  : Integer;    {z   -1..-1023 where '-' is into screen}
  14.   xx : integer; {2d x coord}
  15.   yy : integer;    {y}
  16.  
  17. xx:=vidwidth div 2  + longint(x)*1024 div z;
  18. yy:=vidheight div 2 + longint(y)*1024 div z;
  19. {
  20. That'll give you just plain depth scaling for one star.  For many stars,
  21. just keep an array like this for each of those:
  22. }
  23.   X  : array [1..maxstars] of integer;
  24. {
  25. You'd basically follow this pattern:
  26. }
  27.  for t:=1 to maxstars do
  28.    begin
  29.      {if star is visible, clear it}
  30.      if getpixel(xx[t],yy[t])=starcolor then
  31.        putpixel(xx[t],yy[t],backgroundcolor);
  32.      {update star position}
  33.      whatever math you want.  Maybe just:
  34.      inc(z[t]);
  35.      if z<=0 then
  36.        begin
  37.          x:=random(2048)-1024;
  38.          y:=random(2048)-1024;
  39.          z:=-1024;
  40.        end;
  41.      {translate 3d to 2d}
  42.      xx[tt]:= {etc from above}
  43.      {draw new points}
  44.      if getpixel(xx[t],yy[t])=backgroundcolor then
  45.        putpixel(xx[t],yy[t],starcolor);
  46.    end;
  47. {
  48. of course this won't compile, but I assume you like to code and so I'm
  49. only giving you a general idea.  Then you can put another variable in, a
  50. for example, which is not an array but just a constant which indicated
  51. the "angle" of rotation around the z axis. (spinning)  it's easy to
  52. implement that into the equation without any 3d math stuff.
  53. }
  54. xx[tt]:=longint(x[t])*1024 div z[t] * sintable[a mod 360]
  55.         div (sin table precision constant, usually 256);
  56.  
  57. yy[tt]:=longint(y[t])*1024 div z[t] * costable[a mod 360]
  58.         div 256;
  59.